import currentItenComponent from "./currentItenComponent.vue";
export default {
  name: "addPost",
  data() {
    return {
      addPost: [],
      manage: "新增",
      addRoleForm: {
        departmentid: "",
        department: "", // 部门
        position: "", // 岗位
        powerlist: [], //权限列表(全部)
        rolelist: [], // 权限列表(只包含权限id)
      },
      authorities: [], //权限列表
      selectedRole: [], //选中tab样式
      powerData: [], //权限设置
      buttonList: [], //按钮权限设置
      buttonAction: [], //选中按钮样式
      currentTittle: "", //标题
      btnlistDialogVisible: false,
      powerAll: [], //判断菜单模块是否存在
      currentData: [],
      allTable: [], //判断tab模块是否存在
      editData: {}, //用于编辑
      editTab: {}, //用于编辑
      selectBtn: [],
    };
  },
  components: {
    currentItenComponent,
  },
  props: ["allrolelist", "currentStatus", "select"],
  methods: {
    // 关闭新增/修改岗位角色对话框
    handleCloseRoleDialog() {
      if (this.manage === "新增") {
        this.emptyRole();
      }
    },
    // 取消新增/修改岗位角色对话框
    cancleRoleDialog() {
      // this.addRoleIsVisible = false;
      this.$emit("back");
      if (this.manage === "新增") {
        this.emptyRole();
      }
    },

    // 新增/修改岗位角色
    submitRoleForm() {
      this.addRoleForm.powerlist = this.powerData;
      if (
        this.powerData.length !== 0 &&
        this.addRoleForm.department &&
        this.addRoleForm.position
      ) {
        this.$confirm(`是否${this.manage}该角色?`, "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            // 默认添加概览权限
            // let tempObj = {
            //   permissionid: this.dashboardid
            // }
            // this.addRoleForm.rolelist.push(tempObj);
            this.$axios
              .$post(
                "/system/v1/platform/department/insertAndUpdate",
                this.addRoleForm
              )
              .then((res) => {
                if (res.data.message === "success.") {
                  this.addRoleIsVisible = false;
                  // this.getroleManageData();
                  // this.getpostData();
                  this.$emit("callgetpostData");
                  this.emptyRole();
                  if (this.manage === "新增") {
                    this.$message({
                      message: "新增成功",
                      type: "success",
                    });
                    this.$emit("back");
                  } else if (this.manage === "编辑") {
                    this.$message({
                      message: "编辑成功",
                      type: "success",
                    });
                    this.$emit("back");
                  }
                } else {
                  this.$message({
                    message: res.data.message,
                    type: "warning",
                  });
                }
              });
          })
          .catch(() => {
            this.$message({
              type: "warning",
              message: "已取消该操作",
            });
            this.$emit("back");
          });
      } else {
        this.$message({
          message: "请填选相关信息",
          type: "warning",
        });
      }
    },

    // 清空岗位角色数据
    emptyRole() {
      this.addRoleForm.departmentid = "";
      this.addRoleForm.department = "";
      this.addRoleForm.position = "";
      this.addRoleForm.rolelist = [];
      this.selectedRole = [];
    },
    addBtn(type, tab, data) {
      //无tab
      let obj = {
        power: data.power,
        tablist: [],
      };
      if (this.powerData.length == 0) {
        this.powerData.push(obj);
        this.selectedRole.push(obj.power);
      } else if (this.powerData.length > 0) {
        let idx = this.powerData.findIndex((item) => item.power == data.power);
        let idx1 = this.selectedRole.findIndex((item) => item == data.power);
        if (idx > -1 && idx1 > -1) {
          this.powerData.splice(idx, 1);
          this.selectedRole.splice(idx1, 1);
        } else {
          this.powerData.push(obj);
          this.selectedRole.push(obj.power);
        }
      }
    },
    addTab(data, select) {
      //tab
      console.log(data);
      this.selectedRole = select;

      let obj = {
        power: data.power,
        tablist: [],
      };
      if (this.powerData.length == 0) {
        obj.tablist.push(data.current);
        this.powerData.push(obj);
      } else if (this.powerData.length > 0) {
        var idx = this.powerData.findIndex((iten) => iten.power == data.power);
        if (idx > -1) {
          let el = this.powerData[idx];

          var index = el.tablist.findIndex(
            (iten) => iten.table == data.current.table
          );
          index > -1
            ? el.tablist.splice(index, 1, data.current)
            : el.tablist.push(data.current);
        } else {
          obj.tablist.push(data.current);
          this.powerData.push(obj);
        }
      }
      console.log(obj);
    },
    clearTab(data, select) {
      this.selectedRole = select;
      var idx = this.powerData.findIndex((iten) => iten.power == data.power);
      let el = this.powerData[idx];
      var index = el.tablist.findIndex((iten) => iten.table == data.table);
      index > -1 ? el.tablist.splice(index, 1) : "";
      if (el.tablist.length == 0) {
        //新增时初次选中tab二次点击去掉
        this.powerData.splice(idx, 1);
      }
    },
    editClear(itemData, select) {
      //编辑时全部移除
      this.selectedRole = select;
      let idx = this.powerData.findIndex(
        (item) => item.power == itemData.power
      );
      if (idx > -1) {
        let el = this.powerData[idx];
        let index = el.tablist.findIndex(
          (item) => item.table == itemData.item.table
        );
        el.tablist.splice(index, 1);
        if (this.currentStatus == 2 && el.tablist.length == 0) {
          //新增时初次选中tab二次点击去掉
          this.powerData.splice(idx, 1);
        }
      }
    },
  },
  created() {
    this.authorities = JSON.parse(localStorage.getItem("permisstions"));
    console.log(this.authorities);
    if (this.currentStatus == 2) {
      this.allTable = [];
      this.manage = "编辑";
      this.addRoleForm.departmentid = this.allrolelist.departmentid;
      this.addRoleForm.department = this.allrolelist.department; // 部门
      this.addRoleForm.position = this.allrolelist.position; // 岗位
      this.addRoleForm.rolelist = this.allrolelist.rolelist;
      console.log(this.addRoleForm, "this.addRoleForm.rolelist");
      this.addRoleForm.rolelist.forEach((data) => {
        var obj = {
          power: data.power,
          tablist: [],
        };
        if (data.tablelist.length > 0) {
          data.tablelist.forEach((el) => {
            var currentobj = {
              table: el.table,
              buttonlist: [],
            };
            if (this.allTable.indexOf(currentobj.table) == "-1") {
              this.allTable.push(currentobj.table);
              obj.tablist.push(currentobj);
            }
            el.buttonlist.forEach((currentData) => {
              currentobj.buttonlist.push(currentData.button);
            });
            // obj.tablist.push(currentobj)
          });
        }

        this.powerData.push(obj);
        this.selectBtn = this.powerData;
      });
      // this.addRoleForm.rolelist;

      this.addRoleForm.rolelist.forEach((item) => {
        if (item.tablelist.length == 0) {
          this.selectedRole.push(item.power);
        } else if (item.tablelist.length != 0) {
          item.tablelist.forEach((el) => {
            this.selectedRole.push(el.table);
          });
        }
        if (this.powerAll.indexOf(item.power) == "-1") {
          this.powerAll.push(item.power);
        }
      });
    } else if (this.currentStatus == 1) {
      this.manage = "新增";
      this.addRoleForm = {
        departmentid: "",
        department: "", // 部门
        position: "", // 岗位
        rolelist: [], // 权限列表(只包含权限id)
      };
    }
  },
  mounted() {},
};
